package com.hei.controller;

import com.hei.pojo.Role;
import com.hei.pojo.User;
import com.hei.service.Impl.RoleServiceImpl;
import com.hei.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class RoleController {

    @Resource
    @Autowired
    private RoleService roleService;

    @RequestMapping("/role/showList")
    public String showList(Model model) {
        List<Role> roleList = roleService.showList();
        model.addAttribute("roles", roleList);
        return "/pages/role-list";
    }

    @RequestMapping("/role/addRole")
    public String addRole(Role role) {
        int i = roleService.addRole(role);
        System.out.println(i);
        return "redirect:/role/showList";
    }

    @RequestMapping("/role/userList")
    public String userList(Model model) {
        List<User> users = roleService.showUserList();

        for (User user : users) {
            StringBuilder allJob = new StringBuilder();
            int[] nums = roleService.getJobs(user.getId());
            for(int i = 0; i < nums.length; i++) {
                allJob.append(roleService.getJobName(nums[i]));
                if (i < nums.length - 1) { // 如果不是最后一个，添加逗号
                    allJob.append(", ");
                }
            }
            user.setJobName(allJob.toString());
        }
        model.addAttribute("users", users);
        return "/pages/user-list";
    }

    @RequestMapping("/role/userRole")
    public String addUser_getRole(Model model) {
        List<Role> roleList = roleService.showList();
        model.addAttribute("roles", roleList);
        return "/pages/user-add";
    }

    @RequestMapping("/role/addUser")
    public  String addUser(User user) {
        int i = roleService.addUser(user);

        return "redirect:/role/userList";
    }

    @RequestMapping("/role/userDel")
    public String deleteUser(@RequestParam("id") int id) {
        roleService.deleteUser(id);
        return "redirect:/role/userList";
    }

    @RequestMapping("/role/user-edit")
    public String editUser(@RequestParam("id") int id, Model model) {
        //获取user信息
        User user = roleService.getUser(id);
        //获取user角色数组
        int[] userRoles = roleService.getJobs(id);
        //返回所有角色作为多选框
        List<Role> roleList = roleService.showList();
        model.addAttribute("roles", roleList);
        model.addAttribute("user",user);
        model.addAttribute("userRoles",userRoles);
        return "/pages/user-edit";
    }

    @RequestMapping("/role/updateUser")
    public String updateUser(User user) {
        int i = roleService.updateUser(user);
        return "redirect:/role/showList";
    }

    @RequestMapping("/role/roleDel")
    public String delRole(@RequestParam("id") int id) {
        roleService.deleteRole(id);
        return "redirect:/role/showList";
    }

    @RequestMapping("/role/role-edit")
    public String editRole(@RequestParam("id") int id, Model model) {
        Role role = roleService.getRole(id);
        model.addAttribute("role", role);
        return "/pages/role-edit";
    }

    @RequestMapping("/role/updateRole")
    public String updateRole(Role role) {
        int i = roleService.updateRole(role);
        return "redirect:/role/showList";
    }

    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session,Model model) {
        boolean res = roleService.login(username, password);

        if (res){
            session.setAttribute("username", username);
            return "redirect:/role/showList";
        }else {
            model.addAttribute("用户名或密码错误，请重试");
            System.out.println("controller-fail");
            return "/error";
        }
    }
}
